package com.example.task1.ui.AllService.bus;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.BusOrderBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class BusOrderAdapter extends BaseAdapter<BusOrderBean.RowsBean> {
    private Context mContext;
    private List<BusOrderBean.RowsBean> mData = new ArrayList<>();
    private TextView tvName;
    private TextView tvStatus;
    private TextView tvStart;
    private TextView tvEnd;
    private TextView tvTime;
    private Button btnPlay;

    public BusOrderAdapter(Context context) {
        mContext = context;
    }

    @Override
    protected void setData(List<BusOrderBean.RowsBean> rows) {
        mData = rows;
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.busorderlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<BusOrderBean.RowsBean> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, BusOrderBean.RowsBean date) {
        tvName.setText(date.getPath());
        tvEnd.setText("终点：" + date.getEnd());
        tvStart.setText("起点：" + date.getStart());
        tvTime.setText("下单时间：" + date.getCreateTime());
        tvStatus.setText(date.getStatus() == 0 ? "未支付" : "已支付");
        if (date.getStatus() == 1) {
            btnPlay.setVisibility(View.GONE);
        }
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> put = new HashMap<>();
                put.put("orderNum", date.getOrderNum());
                put.put("paymentType", "电子支付");
                Call<ApiResp> respCall = Api.api().post(MyApplication.TOKEN, "/prod-api/api/bus/pay", Api.getBody(put));
                Api.send(respCall, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("支付成功");
                        ((BusOrderActivity) mContext).finish();
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View view) {
        tvName = view.findViewById(R.id.tv_name);
        tvStatus = view.findViewById(R.id.tv_status);
        tvStart = view.findViewById(R.id.tv_start);
        tvEnd = view.findViewById(R.id.tv_end);
        tvTime = view.findViewById(R.id.tv_time);
        btnPlay = view.findViewById(R.id.btn_play);
    }
}
